﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Team4Mock.Entity;
namespace Team4Mock.UserControl.Organisation
{
    public partial class ViewAll : System.Web.UI.UserControl
    {
        OrganisationEntity oe=new OrganisationEntity();
        SupportEntity s=new SupportEntity();
        DirectorateEntity d=new DirectorateEntity();
        PremiseEntity p=new PremiseEntity();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Convert.ToString(Session["role"])))
                {
                    if (Session["role"].ToString().Equals("Admin"))
                    {
                        btnDel.Visible = true;
                    }
                    else
                    {
                        btnDel.Visible = false;
                    }
                }
                else
                {
                    btnDel.Visible = false;
                }
                CheckBoxList11.Items.Add("dasdsasad");
                CheckBoxList11.Items.Add("dasdsasad");
                CheckBoxList11.Items.Add("dasdsasad");
                if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
                {
                    pnct.Visible = true;
                    pngv.Visible = false;
                    pnCreate.Visible = false;

                    int id = int.Parse(Request.QueryString["ID"]);
                    DataTable dt=new DataTable();
                    oe.Organizationid = id;
                    dt = oe.GetDataById(oe);
                    for (int o = 0; o < dt.Rows.Count; o++)
                    {
                        txtOrgName.Text = dt.Rows[0][1].ToString();
                        txtShortDes.Text = dt.Rows[0][2].ToString();
                        idContact.Value= dt.Rows[0][3].ToString();
                        txtAdd1.Text = dt.Rows[0][4].ToString();
                        txtAdd2.Text = dt.Rows[0][5].ToString();
                        txtAdd3.Text = dt.Rows[0][6].ToString();
                        PostCode.Value = dt.Rows[0][7].ToString();
                        Town.Value = dt.Rows[0][8].ToString();
                        County.Value = dt.Rows[0][9].ToString();
                        dlNational.SelectedValue = dt.Rows[0][10].ToString();
                        if (dt.Rows[0][11].ToString()=="1")
                        {
                            ckPreferred.Checked = true;
                        }
                        else
                        {
                            ckPreferred.Checked = false;
                        }
                        if (dt.Rows[0][12].ToString() == "1")
                        {
                            ckExpress.Checked = true;
                        }
                        else
                        {
                            ckExpress.Checked = false;
                        }
                        hdidBus.Value = dt.Rows[0][13].ToString();
                        hdsic.Value = dt.Rows[0][14].ToString();
                        txtFullDes.Text = dt.Rows[0][15].ToString();
                        txtPhone.Text = dt.Rows[0][16].ToString();
                        txtFax.Text = dt.Rows[0][17].ToString();
                        txtWeb.Text = dt.Rows[0][18].ToString();
                        txtCharity.Text = dt.Rows[0][19].ToString();
                        txtCompany.Text = dt.Rows[0][20].ToString();
                        if (dt.Rows[0][21].ToString()=="1")
                        {
                            ckIsactive.Checked = true;
                        }
                        else
                        {
                            ckIsactive.Checked = false;
                        }
                    }
                    DataTable dt1=new DataTable();
                    DataTable dt2 = new DataTable();
                    dt1 = oe.GetAccById(oe);
                    dt2 = oe.GetAccred();
                    string a = "", b = "";
                    for (int i = 0; i < dt2.Rows.Count; i++)
                    {
                        for (int j = 0; j < dt1.Rows.Count; j++)
                        {
                             a = dt1.Rows[j][0].ToString();
                            b = dt2.Rows[j][0].ToString();
                            if (a==b)
                            {
                                CheckBoxList11.Items.Add(a);
                            }
                        }
                    }
                    rpAcc.DataSource = oe.GetAccById(oe);
                    rpAcc.DataBind();
                    
                    rpBene.DataSource = oe.GetBenefitById(oe);
                    rpBene.DataBind();

                    rpOrgSpec.DataSource = oe.GetSpecById(oe);
                    rpOrgSpec.DataBind();

                    rpSerBarr.DataSource = oe.GetBarriersById(oe);
                    rpSerBarr.DataBind();

                    rpSerDisCap.DataSource = oe.GetDisaById(oe);
                    rpSerDisCap.DataBind();

                    rpSerEth.DataSource = oe.GetEthnicById(oe);
                    rpSerEth.DataBind();

                    rpSerPerCap.DataSource = oe.GetPersoById(oe);
                    rpSerPerCap.DataBind();

                    dlNational.DataSource = oe.GetCountry();
                    dlNational.DataBind();

                    s.OrganizationID = id;

                    gvSupport.DataSource = s.GetDataById(s);
                    gvSupport.DataBind();

                    d.OrganizationID = id;
                    gvDirectorate.DataSource = d.GetDataByID(d);
                    gvDirectorate.DataBind();

                    p.ID = id;
                    gvPremise.DataSource = p.GetDataById(p);
                    gvPremise.DataBind();
                }
                else
                {
                    pnct.Visible = false;
                    pngv.Visible = true;
                    pnCreate.Visible = false;

                    gvOrg.DataSource = oe.Getdata();
                    gvOrg.DataBind();
                }
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                if (txtOrgName.Text == "" || txtShortDes.Text == "" || txtAdd1.Text == "" || txtPhone.Text == "" ||
                    hdidBus.Value == "" || hdsic.Value == "" || PostCode.Value == "")
                {
                    Label2.Text = "Input Mandatory fields";
                }
                else
                {
                    int contactid = int.Parse(idContact.Value);
                    string idbus = hdidBus.Value;
                    string postcode = PostCode.Value;
                    string sic = hdsic.Value;
                    string town = Town.Value;
                    string county = County.Value;
                    string national = dlNational.SelectedValue;

                    oe.OrganizationName = txtOrgName.Text.Trim();
                    oe.OrganizationShortDescription = txtShortDes.Text;
                    oe.LeadContact = contactid;
                    if (ckPreferred.Checked == true)
                        oe.PreferredOrganization = 1;
                    else
                        oe.PreferredOrganization = 0;
                    if (ckExpress.Checked == true)
                        oe.ExpressionOfOrganization = 1;
                    else
                        oe.ExpressionOfOrganization = 0;
                    oe.TypeOfBusiness = idbus;
                    oe.SICCode = sic;
                    oe.OrFullDescription = txtFullDes.Text;
                    oe.AddressLine1 = txtAdd1.Text;
                    oe.AddressLine2 = txtAdd2.Text;
                    oe.AddressLine3 = txtAdd3.Text;
                    oe.PostCode = postcode;
                    oe.Town = town;
                    oe.County = county;
                    oe.Country = national;
                    oe.PhoneNumber = txtPhone.Text;
                    oe.Fax = txtFax.Text;
                    oe.WebAddress = txtWeb.Text;
                    oe.ChartyNumber = txtCharity.Text;
                    oe.CompanyNumber = txtCompany.Text;
                    oe.Organizationid = int.Parse(Request.QueryString["ID"]);
                    oe.IsActive = ckIsactive.Checked ? 1 : 0;
                    oe.Update(oe);
                    Label2.Text = "Update Success";
                }
            }

        }
        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("/FormView/Organisation.aspx");
        }
        public SortDirection SortDirection
        {
            get
            {
                if (ViewState["SortDirection"] == null)
                {
                    ViewState["SortDirection"] = SortDirection.Ascending;
                }
                return (SortDirection)ViewState["SortDirection"];
            }
            set
            {
                ViewState["SortDirection"] = value;
            }
        }

        protected void gvOrg_Sorting(object sender, GridViewSortEventArgs e)
        {
            DataTable dt = new DataTable();
            dt = oe.Getdata();

            string sortExpression = e.SortExpression;
            string direction = string.Empty;
            string sort = "";
            if (SortDirection == SortDirection.Ascending)
            {
                SortDirection = SortDirection.Descending;
                direction = " DESC";
                sort = "↑";

            }
            else
            {
                SortDirection = SortDirection.Ascending;
                direction = " ASC";
                sort = "↓";
            }
            if (sortExpression == "Name")
            {
                gvOrg.Columns[1].HeaderText = "Organisation Name " + sort;
                gvOrg.Columns[2].HeaderText = "Head Office Address Line 1";
                gvOrg.Columns[3].HeaderText = "Post Code";
                gvOrg.Columns[4].HeaderText = "Contact";
                gvOrg.Columns[5].HeaderText = "Is Active";
            }
            if (sortExpression == "Address")
            {
                gvOrg.Columns[1].HeaderText = "Organisation Name";
                gvOrg.Columns[2].HeaderText = "Head Office Address Line 1 " + sort;
                gvOrg.Columns[3].HeaderText = "Post Code";
                gvOrg.Columns[4].HeaderText = "Contact";
                gvOrg.Columns[5].HeaderText = "Is Active";
            }
            if (sortExpression == "Postcode")
            {
                gvOrg.Columns[1].HeaderText = "Organisation Name";
                gvOrg.Columns[2].HeaderText = "Head Office Address Line 1";
                gvOrg.Columns[3].HeaderText = "Post Code " + sort;
                gvOrg.Columns[4].HeaderText = "Contact";
                gvOrg.Columns[5].HeaderText = "Is Active";
            }
            if (sortExpression == "Contact")
            {
                gvOrg.Columns[1].HeaderText = "Organisation Name";
                gvOrg.Columns[2].HeaderText = "Head Office Address Line 1";
                gvOrg.Columns[3].HeaderText = "Post Code";
                gvOrg.Columns[4].HeaderText = "Contact " + sort;
                gvOrg.Columns[5].HeaderText = "Is Active";
            }
            if (sortExpression == "IsActive")
            {
                gvOrg.Columns[1].HeaderText = "Organisation Name";
                gvOrg.Columns[2].HeaderText = "Head Office Address Line 1";
                gvOrg.Columns[3].HeaderText = "Post Code";
                gvOrg.Columns[4].HeaderText = "Contact";
                gvOrg.Columns[5].HeaderText = "Is Active " + sort;
            }
            dt.DefaultView.Sort = sortExpression + direction;

            gvOrg.DataSource = dt;
            gvOrg.DataBind();
        }

        protected void gvPremise_Sorting(object sender, GridViewSortEventArgs e)
        {
            DataTable dt2 = new DataTable();
            dt2.Columns.Add("ID", typeof(int));
            dt2.Columns.Add("Premise Name");
            dt2.Columns.Add("Address");
            dt2.Columns.Add("Primary Location");
            dt2.Columns.Add("Phone Number");

            dt2.Rows.Add(1, "ZAdsadsadasdas", "Ha Noi", "dasdasd", "979779");
            dt2.Rows.Add(2, "CBsadsadasdas", "Viet Nam", "dasdasd", "543534");
            dt2.Rows.Add(3, "FBdsadsadasdas", "Ha Noi", "dasdasd", "32243");
            dt2.Rows.Add(4, "GDdsadsadasdas", "Ha Noi", "dasdasd", "423423");
            dt2.Rows.Add(5, "AEdsadsadasdas", "Hung Yen", "dasdasd", "7567567");
            dt2.Rows.Add(6, "KFdsadsadasdas", "Hung Yen", "dasdasd", "234234");
            dt2.Rows.Add(7, "BGdsadsadasdas", "Hung Yen", "dasdasd", "6456546");

            string ad = e.SortExpression.ToString();
            SortDirection sort = new SortDirection();
            string direc = "";
            if (sort == SortDirection.Ascending)
                direc = " ASC";
            if (sort == SortDirection.Descending) direc = " DESC";

            dt2.DefaultView.Sort = ad + direc;
            gvPremise.DataSource = dt2;
            gvPremise.DataBind();
        }
        protected void btnActive_Click(object sender, EventArgs e)
        {

        }
        protected void btnCreate_Click(object sender, EventArgs e)
        {
            pnct.Visible = false;
            pngv.Visible = false;
            pnCreate.Visible = true;
        }
        protected void btnBack1_Click(object sender, EventArgs e)
        {
            pnct.Visible = false;
            pngv.Visible = true;
            pnCreate.Visible = false;
        }



        protected void gvOrg_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private DataTable dt;
        protected void All_Click(object sender, EventArgs e)
        {
            dt = oe.Getdata();
            if (dt.Rows.Count>0)
            {
                gvOrg.DataSource = dt;
                gvOrg.DataBind();
                abc.Text = "";
            }
            else
            {
                abc.Text = "No Record";
            }
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            oe.Key = "ABCDE";
            dt = oe.Search(oe);
            if (dt.Rows.Count > 0)
            {
                gvOrg.DataSource = dt;
                gvOrg.DataBind();
                abc.Text = "";
            }
            else
            {
                abc.Text = "No Record";
            }
        }

        protected void LinkButton3_Click(object sender, EventArgs e)
        {
            oe.Key = "FGHIJ";
            dt = oe.Search(oe);
            if (dt.Rows.Count > 0)
            {
                gvOrg.DataSource = dt;
                gvOrg.DataBind();
                abc.Text = "";
            }
            else
            {
                abc.Text = "No Record";
            }
        }

        protected void LinkButton4_Click(object sender, EventArgs e)
        {
            oe.Key = "KMNL";
            dt = oe.Search(oe);
            if (dt.Rows.Count > 0)
            {
                gvOrg.DataSource = dt;
                gvOrg.DataBind();
                abc.Text = "";
            }
            else
            {
                abc.Text = "No Record";
            }
        }

        protected void LinkButton5_Click(object sender, EventArgs e)
        {
            oe.Key = "OPQR";
            dt = oe.Search(oe);
            if (dt.Rows.Count > 0)
            {
                gvOrg.DataSource = dt;
                gvOrg.DataBind();
                abc.Text = "";
            }
            else
            {
                abc.Text = "No Record";
            }
        }

        protected void LinkButton6_Click(object sender, EventArgs e)
        {
            oe.Key = "STUV";
            dt = oe.Search(oe);
            if (dt.Rows.Count > 0)
            {
                gvOrg.DataSource = dt;
                gvOrg.DataBind();
                abc.Text = "";
            }
            else
            {
                abc.Text = "No Record";
            }
        }

        protected void LinkButton7_Click(object sender, EventArgs e)
        {
            oe.Key = "WXYZ";
            dt = oe.Search(oe);
            if (dt.Rows.Count > 0)
            {
                gvOrg.DataSource = dt;
                gvOrg.DataBind();
                abc.Text = "";
            }
            else
            {
                abc.Text = "No Record";
            }
        }

        protected void LAll_Click(object sender, EventArgs e)
        {

        }

        protected void LAE_Click(object sender, EventArgs e)
        {

        }

        protected void LFJ_Click(object sender, EventArgs e)
        {

        }

        protected void LKL_Click(object sender, EventArgs e)
        {

        }

        protected void LOR_Click(object sender, EventArgs e)
        {

        }

        protected void LSV_Click(object sender, EventArgs e)
        {

        }

        protected void LWZ_Click(object sender, EventArgs e)
        {

        }

        protected void ckActive_CheckedChanged(object sender, EventArgs e)
        {
            if (ckActive.Checked==true)
            {
                gvOrg.DataSource = oe.Getdata();
                gvOrg.DataBind();
                
            }
            else
            {
                gvOrg.DataSource = oe.GetdataTrue();
                gvOrg.DataBind();
            }
        }

        protected void btnCreateSup_Click(object sender, EventArgs e)
        {
            Response.Redirect("/FormView/Support.aspx?Org="+Request.QueryString["ID"]);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("/FormView/Directorate.aspx?Org=" + Request.QueryString["ID"]);
        }

        protected void gvOrg_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            int page = e.NewPageIndex;
            gvOrg.PageIndex = page;
        }

        protected void btnDel_Click(object sender, EventArgs e)
        {
            try
            {
                int id = int.Parse(Request.QueryString["ID"]);
                oe.Organizationid = id;
                oe.Del(oe);
                Response.Redirect("/FormView/Organisation.aspx"); 
            }
            catch
            {
                tb.Text = "Error while being processed";
            }
        }
    }
}